package com.gujh.security.controller;

import cn.hutool.core.util.RandomUtil;
import com.gujh.security.model.Res;
import com.gujh.security.model.SmsCaptchaVo;
import com.gujh.security.util.CacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

// 获取验证码的接口
@RestController
@RequestMapping("/sms")
@Slf4j
public class SmsController {

    /**
     * 发送验证码接口
     */
    @GetMapping("/send/Captcha")
    public Res<SmsCaptchaVo> msmCaptcha(@RequestParam String phone) {
        // 1. 获取到手机号
        log.info(phone + "请求获取验证码");
        String code = RandomUtil.randomNumbers(6);
        log.info(phone + "生成验证码：" + code);
        CacheUtil.putCode(phone, code);
        // 3. 返回过期时间，方便前端提示多少时间内有效
        SmsCaptchaVo smsCaptcha = new SmsCaptchaVo();
        smsCaptcha.setPhone(phone);
        smsCaptcha.setExpire(5);
        return Res.success(smsCaptcha, "短信验证码发送成功");
    }
}

